﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HamsterEngine.SimElements;
using System.Drawing;

namespace HamsterEngine
{
    public class Territorium
    {
        private static SimElement[,] fields;
        private static Körner[,] körner;

        public static int Breite
        {
            get
            {
                return fields.GetLength(0);
            }
        }
        public static int Höhe
        {
            get
            {
                return fields.GetLength(1);
            }
        }
        public static Hamster[] Hamster
        {
            get
            {
                if (fields == null) return new List<Hamster>().ToArray();
                List<Hamster> hamster = new List<Hamster>();
                for (int i = 0; i < Breite; i++)
                    for (int j = 0; j < Höhe; j++)
                        if (fields[i, j] is Hamster)
                            hamster.Add(fields[i, j] as Hamster);
                return hamster.ToArray();
            }
        }

        static Territorium()
        {
            new Territorium(8, 8);
        }

        private Territorium(int width, int height)
        {
            Hamster standardHamster = null;
            if (Hamster.Length > 0)
            {
                standardHamster = Hamster[0];
            }

            fields = new SimElement[width, height];

            if (standardHamster != null)
            {
                fields[standardHamster.Position.X, standardHamster.Position.Y] = standardHamster;
            }
            else
            {
                fields[0, 0] = SimElements.Hamster.CreateInstance(0, 0, Color.Blue);
            }

            körner = new Körner[width, height];
            for (int i = 0; i < width; i++)
                for (int j = 0; j < height; j++)
                    körner[i, j] = new Körner(0);
        }

        internal static void SetField(int x, int y, SimElement obj)
        {
            if (obj is Körner)
                körner[x, y] = obj as Körner;
            else
                fields[x, y] = obj;
        }

        public static SimElement GetField(int x, int y)
        {
            if (x < 0 || y < 0 || x >= Breite || y >= Höhe)
                return new Mauer();
            return fields[x, y];
        }

        public static Körner GetKörner(int x, int y)
        {
            return körner[x, y];
        }
    }
}
